<?
if (!$isInstallProgram)
	exit;
$admin_area=1;
include "admin/config/gen_inc.php";
$lFileContent=file("admin/setup/db_up.txt");
$lTableArray=getTables();

$lCharSetString="CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$lRes=q("SELECT VERSION() as mysql_version");
$lRowVersion=mysql_fetch_array($lRes);
$lMySQLVersion=$lRowVersion["mysql_version"];
$lMySQLVersionArray=split("\.",$lMySQLVersion);

foreach ($lMySQLVersionArray as $lItem)
{
		$lNum=round($lItem);
		$lVerArray[]=$lNum;
}
$lMajor=$lVerArray[0];
$lSub=$lVerArray[1];
if ($lMajor !=5)
{
	if ($lMajor==4 && $lSub==0)
		$lSkipUTF=1;
	if ($lMajor==3)
		$lSkipUTF=1;
}

if ($lSkipUTF==1)
{
	$lCharSetString="";
	echo "<p>You have a MYSQL version ($lMySQLVersion) that doesn't support UTF8 character set. Upgrade to v4.1 or 5.x <strong>if</strong> you have non-latin characters in your language. Later editions of PHP Classifieds may require v5.</p>";
}
elseif ($lMajor>5)
{
	echo "<p>You have MySQL version $lMySQLVersion. We reccomend MySQL version 5.0 and higher due to speed improvements and functionality. Also, later editions of the program may only support MySQL 5</p>";
}


foreach ($lFileContent as $lLine) {
	$i++;
	// First two lines are administrative issues
	
	if ($i==1)
	{
		$lDataArray=split(":",$lLine);
		$lVersion=trim($lDataArray[0]);
		$lDate=$lDataArray[1];
		
		//echo $lVersion;
	}
	elseif ($i==2)
	{}
	else 
	{
		
		// Correct Values		
		$lArray=split("\|",$lLine);
		$lTableName=trim($lArray[0]);
		$lTableName=eregi_replace("phpclass_","",$lTableName);
		$lTableName=$set_table_prefix . $lTableName;
		$lKey=trim($lArray[1]);
		$lFieldName=trim($lArray[2]);
		$lFieldProp=trim($lArray[3]);
		$lFieldExtra=trim($lArray[4]);
		
		if ($lTableName != $lastTableCreated)
		{
			echo "<img src='layout_images/admin/check.gif' hspace='3' align='left'>Checking $lTableName<br><br>";
			if (checkIfExists($lTableArray,$lTableName)==false)
			{
				$sql="create table $lTableName ($lFieldName $lFieldProp) $lCharSetString;";
				$res=q($sql);
				if ($lShowAction)
					echo "Created $lTableName<br>";
				
				
			}
			$lastTableCreated=$lTableName;
		}
		if ($lShowAction)
			echo "<b>Checking FIELD $lFieldName</b><hr>";
		$lFound=0;
		$lCorrectType=0;
		
		
		// Loop through db array, look for missing fields
		$lFieldArray=getAllTables();
		if (is_array($lFieldArray))
		{
			foreach ($lFieldArray as $lItem)
			{
				if ($lItem["Table"]==$lTableName)
					if ($lItem["Field"]==$lFieldName)
						$lFound=1;
			}	
		}
		// If we did not find the field in your DB
		if (!$lFound)
		{
			
			if (!$lFound)			
			{
				if ($lFieldProp=="text")
					$sql="alter table $lTableName add $lFieldName $lFieldProp $lCharSetString DEFAULT NULL;";
				elseif ($lFieldProp=="decimal(10,2)")
					$sql="alter table $lTableName add $lFieldName $lFieldProp DEFAULT NULL;";
				elseif ($lFieldProp=="timestamp")
					$sql="alter table $lTableName add $lFieldName $lFieldProp;";
				else 
				{
					if (ereg("varchar",$lFieldProp))
						$sql="alter table $lTableName add $lFieldName $lFieldProp $lCharSetString DEFAULT 0;";
					else
						$sql="alter table $lTableName add $lFieldName $lFieldProp DEFAULT 0;";
					
				}
				$res2=q($sql);
				if ($lShowAction)
					echo "Created $lFieldName<br>";
			}
			
		}
		
		// Get current key in db
		$lCurrentDBKey=getIndex($lTableName);
		
		// If we did not find correct fieldtype
		if ($lCurrentDBKey!=$lFieldName AND $lKey=="P")
		{
			if (!empty($lCurrentDBKey))
			{
				// Remove auto-increment value
				$sql="alter table $lTableName CHANGE $lCurrentDBKey $lCurrentDBKey int(11);";
				$res2=q($sql);
				
				// Drop primary key
				$sql="alter table $lTableName DROP PRIMARY KEY";
				$res2=q($sql);
			}
			// Make primary key
			$sql="alter table $lTableName CHANGE $lFieldName $lFieldName $lFieldProp NOT NULL";
			$res2=q($sql);
			$sql="alter table $lTableName ADD PRIMARY KEY($lFieldName)";
			$res2=q($sql);
			
			if ($lShowAction)
				echo "Created Key of field $lFieldName<br>";
		}
		
		
		// Loop through db array, look for wrong properties
		if (is_array($lFieldArray))
		{
			foreach ($lFieldArray as $lItem)
			{
				if ($lItem["Table"]==$lTableName)
					if ($lItem["Field"]==$lFieldName)
					{
						if ($lItem["Type"]==$lFieldProp && $lItem["Extra"]==$lFieldExtra)
							$lCorrectType=1;
							
					}
			}	
		}
			
		// If we did not find correct fieldtype
		if (!$lCorrectType)
		{
			if ($lFieldProp=="text" || $lFieldExtra=="auto_increment")
				$sql="alter table $lTableName modify $lFieldName $lFieldProp $lFieldExtra;";
			elseif ($lFieldProp=="timestamp")
				$sql="alter table $lTableName modify $lFieldName $lFieldProp $lFieldExtra;";
			elseif ($lFieldProp=="decimal(10,2)")
				$sql="alter table $lTableName modify $lFieldName $lFieldProp $lFieldExtra DEFAULT NULL;";
			else 
				$sql="alter table $lTableName modify $lFieldName $lFieldProp $lFieldExtra DEFAULT 0;";
			
			$res2=q($sql);
			if ($lShowAction)
				echo "Corrected field property $lFieldName<br>";
		}
		
		
		
	}
	
	
	
}

// Tell db what version we have upgraded to..
$sql="insert into " . $set_table_prefix . "service (ser_db_version,ser_program_version) values ('$lVersion','$lVersion')";
$res=q($sql);
?>